package com.chinapost.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * 备份的操作类，这个类中存在4个主要的方法
 * 
 * @author Administrator
 * 
 */
public class ImportFileUtil {
	private static ImportFileUtil util;
	/**
	 * 上传的数据文件名
	 */
	private String filename;
	/**
	 * 硬盘上的真实路径，可以在web项目中传入
	 */
	private static String realPath;
	/**
	 * 数据库名称
	 */
	private String database;
	/**
	 * 数据库的用户名
	 */
	private String username;
	/**
	 * 数据库的用户密码
	 */
	private String password;
	/**
	 * 日志文件夹路径
	 */
	private String logFile;
	/**
	 * 日志文件名
	 */
	private final static String LOG_NAME = "log";
	/**
	 * 日志文件夹路径名称
	 */
	private final static String BACKUP_NAME = "log";

	/**
	 * 会自动读取classpath中import.properties文件。
	 * 
	 * @throws IOException
	 */
	private ImportFileUtil() throws IOException {
		Properties prop = new Properties();
		prop.load(ImportFileUtil.class.getClassLoader().getResourceAsStream("import.properties"));
		database = prop.getProperty("database");
		username = prop.getProperty("database_username");
		password = prop.getProperty("database_password");
		logFile = prop.getProperty("logFile");
		File logDir = new File(realPath + File.separator + logFile);
		if (!logDir.exists())
			logDir.mkdirs();
	}

	public static ImportFileUtil getInstance(String realPath) {
		try {
			ImportFileUtil.realPath = realPath;
			if (util == null)
				util = new ImportFileUtil();
			return util;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 从文本文件中导入数据到oracle数据库中
	 * 
	 * @param tablename(TDQCD,TYKT,TYBT)
	 * @param datafile--上传的数据文件的全路径名
	 * ctlfile --sql loader需要的控制文件名（dqcd.ctl,ybt.ctl....)
	 * badfile--sql loader生成的bad文件名
	 */
	public boolean importData(String tableName,String datafile, String ctlfile ,String badfile) {
		ImportUtil util = ImportUtil.getInstance();
		 SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); 
		 //生成一个临时控制文件
		String outfilename = realPath+File.separator+df.format(new Date())+"_"+tableName+".ctl";
		try {
			getCtlfile(tableName,datafile,ctlfile, outfilename,badfile);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String logfile = realPath + File.separator + logFile+File.separator+df.format(new Date())+"_"+tableName+".log";
		util.setCfg(datafile, outfilename, logfile, database, username, password);
		return util.importData();
	}

	/**
	 * 根据控制文件模板，添加参数生成一个临时的控制文件用于sql loader导入
	 * @param tableName
	 * @param datafile
	 * @param infilename
	 * @param outfilename
	 * @param badfile
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	private void getCtlfile(String tableName,String datafile,String infilename,String outfilename,String badfile) throws IOException, UnsupportedEncodingException {
		File infile = new File(infilename);
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(infile)));
		File outfile = new File(outfilename);
		PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile))));
		String reading;
		while ((reading = in.readLine()) != null) {
			//替换中间“###”这行
			if ("###".equals(reading)){
			out.println("INFILE \'"+datafile+"\'");
			out.println("BADFILE \'"+badfile+"\'");
			out.println("INTO TABLE "+tableName);
			}else{
				out.println(reading);
			}
			
		}
		out.close();
		in.close();
	}
}
